$(function () {
    var myEchart = echarts.init(document.getElementById('myChart'));

    layui.use(['element', 'layer'], function () {
        var element = layui.element;
        var layer = layui.layer;

        //监听折叠
        /* element.on('collapse(test)', function (data) {
             layer.msg('展开状态：' + data.show);
         });*/
    });

    $.ajax({
        type: "POST",
        url: "/item/itemCensus",
        dataType: "json",
        success: function (res) {
            // console.log(res.data);

            myEchart.setOption({
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',    // 设置图表类型为饼图
                        radius: '55%',  // 饼图的半径，外半径为可视区尺寸（容器高宽中较小一项）的 55% 长度。
                        data: res.data

                    }
                ]
            })


        }
    });
    var map = new BMapGL.Map('container'); // 创建Map实例
    //发起ajax请求获得用户的address和coordinate
    //根据当前登录用户ID查询address和coordinate
    map.centerAndZoom(new BMapGL.Point(104.241, 30.461), 6); // 初始化地图,设置中心点坐标和地图级别

    map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放
    $.ajax({
        type: "POST",
        url: "/item/bdmap",
        dataType: "json",
        success: function (res) {
            //  console.log(res);
            // var points = new Array();
            // var markers = new Array();
            // var infoWindows = new Array();

            var duixiangs = [];
            for (var i of res.data) {
                var duixiang = {};
                var coo = [];
                coo = i.coordinate.split(",");
                var point = new BMapGL.Point(parseFloat(coo[0]), parseFloat(coo[1]));
                //创建红点
                var marker = new BMapGL.Marker(point);
                var opts = {
                    width: 200,     // 信息窗口宽度
                    height: 100,     // 信息窗口高度
                    title: "省份：" + i.address, // 信息窗口标题
                    message: ""
                }
                // 将标注添加到地图中
                //需要每个省用户的数量
                var infoWindow = new BMapGL.InfoWindow("共" + i.count + "位用户", opts);  // 创建信息窗口对象
                duixiang.point = point;
                duixiang.marker = marker;
                duixiang.address = i.address;
                duixiang.infoWindow = infoWindow;
                duixiangs.push(duixiang);
                // 创建标注
                map.addOverlay(marker);

            }
            // console.log(duixiangs);
            $.each(duixiangs, function (index, value) {

                value.marker.addEventListener("click", function () {
                    map.openInfoWindow(value.infoWindow, value.point); //开启信息窗口

                });
            });

        }
    });


});

